#include <iostream>
#include <signal.h>
#include <unistd.h>

void handler(int signum)
{
    std::cout << "信号捕捉到：" << signum << std::endl;
    // 循环打印pending表，观察2号信号屏蔽情况
    while (true)
    {
        sigset_t set;
        sigpending(&set);
        for (int i = 31; i >= 1; i--)
        {
            if (sigismember(&set, i))
                std::cout << "1";
            else
                std::cout << "0";
        }
        std::cout << std::endl;
        sleep(1);
    }
}

int main()
{
    // signal(2, handler);
    struct sigaction act, oact;
    // 此时额外的屏蔽信号集为{3,4,5}
    sigemptyset(&(act.sa_mask));
    sigaddset(&act.sa_mask,3);
    sigaddset(&act.sa_mask,4);
    sigaddset(&act.sa_mask,5);
    // 自定义捕捉方法
    act.sa_handler = handler;
    act.sa_flags = 0;
    sigaction(2, &act, &oact);

    while (true)
    {
        std::cout << "pid: " << getpid() << std::endl;
        sleep(1);
    }
    return 0;
}